﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Game
{
    class Game
    {
        static int playfieldSize = 20;
        static char[] c = { '#', '@', '#', '*' };
        static Object dwarf = new Object();
        static List<Object> rocks = new List<Object>();
        static Object rock = new Object();
       

        static void SetInitialField()
        {
            Console.BufferHeight = Console.WindowHeight = 30;
            Console.BufferWidth = Console.WindowWidth = 40;

        }

        static void Main()
        {
            SetInitialField();

           
            dwarf.symbol = 'O';
            dwarf.x = playfieldSize / 2;
            dwarf.y = Console.WindowHeight - 1;
            dwarf.color = ConsoleColor.Red;

            Random rand = new Random();

            

            while (true)
            {
                
                rock.symbol = c[rand.Next(0, c.Length)];
                rock.x = rand.Next(0, playfieldSize / 2);
                rock.y = 0;
                rock.color = ConsoleColor.Magenta;
                rocks.Add(rock);
            

                //Move our dwarf(pressed key)
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                    }
                    if (pressedKey.Key == ConsoleKey.LeftArrow)
                    {
                        if (dwarf.x - 1 >= 0)
                        {
                            dwarf.x--;
                        }

                    }
                    else if (pressedKey.Key == ConsoleKey.RightArrow)
                    {
                        if (dwarf.x + 1 < playfieldSize)
                        {
                            dwarf.x++;
                        }
                    }
                }

                //Move rocks
                MoveRocks();
                
                //Check if the dwarf is hit             
                //Clear    
                Console.Clear();
                //Draw playfield --> our player and rocks
                DrawDwarf();
                DrawRock();
                
                
                //Draw info           
                //Slow down program
                
                Thread.Sleep(400);
            }
           
        }

        public static void MoveRocks()
        {
            List<Object> newList = new List<Object>();
            for (int i = 0; i < rocks.Count; i++)
            {
                Object newRocks = new Object();
                newRocks.x = rocks[i].x;
                newRocks.y = rocks[i].y + 1;
                newRocks.color = rocks[i].color;
                newRocks.symbol = rocks[i].symbol;
                //rocks.Remove(oldRocks);

                newList.Add(newRocks);
            }
            rocks = newList;
        }

          public static void DrawRock()
        {
            foreach (Object newRock in rocks)
            {
                Object.PrintOnPosition(newRock.x, newRock.y, newRock.symbol, newRock.color);
            }
        }

        public static void DrawDwarf()
        {
            Object.PrintOnPosition(dwarf.x, dwarf.y, dwarf.symbol, dwarf.color);
        }
    }

}   


